package com.ch.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ch.web.entity.XyScEntity;
import com.ch.web.mapper.XyScDao;
import com.ch.web.redis.UserMapCache;
import com.ch.web.service.XyScService;
import com.ch.web.utils.PageUtils;
import com.ch.web.utils.Query;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Service("xyScService")
public class XyScServiceImpl extends ServiceImpl<XyScDao, XyScEntity> implements XyScService {

    @Autowired
    UserMapCache userMapCache;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<XyScEntity> page = this.page(
                new Query<XyScEntity>().getPage(params),
                new QueryWrapper<XyScEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Boolean getIsSc(Long talkId, Long currentUserId) {
        QueryWrapper<XyScEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("talk_id", talkId).eq("user_id", currentUserId);
        return ObjectUtils.isNotEmpty(this.getOne(wrapper));
    }

    @Override
    public boolean scTalk(XyScEntity xyScEntity, HttpServletRequest request) {
        xyScEntity.setUserId(userMapCache.getUserIdByToken(request));
        return this.save(xyScEntity);
    }

    @Override
    public boolean cancelScTalk(XyScEntity xyScEntity, HttpServletRequest request) {
        QueryWrapper<XyScEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("talk_id", xyScEntity.getTalkId()).eq("user_id", userMapCache.getUserIdByToken(request));
        return this.remove(wrapper);
    }
}